package controller

import (
	"github.com/gin-gonic/gin"
	"tem/dto/controllerDto"
	"tem/middlewares"
	"tem/model"
)

type LikeController struct {
}

func (LikeController) Create(c *gin.Context) {
	var likeDto controllerDto.Like
	err := middlewares.DefaultValidator(c, &likeDto)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  err.Error(),
		})
	} else {
		id, _ := c.Get("id")
		var l model.Like
		l.UserId = uint(id.(int))
		l.InstantId = likeDto.InstantId
		if likeDto.Flag == 1 {
			err := model.Like{}.Delete(l)
			if err != nil {
				c.JSON(200, gin.H{
					"code": 1,
					"msg":  "操作失败",
				})
			} else {
				c.JSON(200, gin.H{
					"code": 0,
					"msg":  "操作成功",
				})
			}
		} else {
			tx := model.Like{}.Create(l)
			if tx.RowsAffected == 1 {
				c.JSON(200, gin.H{
					"code": 0,
					"msg":  "操作成功",
				})
			} else {
				c.JSON(200, gin.H{
					"code": 1,
					"msg":  "操作失败",
				})
			}
		}
	}
}
